<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :title="title"
    width="40%">
    <dynForm
      ref="dataForm"
      :is-inline="false"
      :info-form="infoForm"
      :search-tags="searchTags"
      :select-list="selectList"
      :rules="rules"/>
    <span slot="footer">
      <el-button
        icon="el-icon-circle-close"
        @click="handleClose"
      >{{ $t("cancel") }}</el-button>
      <el-button
        v-btnThrottle="doSave"
        type="primary"
        icon="el-icon-receiving"
      >{{ $t("save") }}</el-button>
  </span></el-dialog>
</template>

<script>
import { getInfo, doAdd, doUpdate } from '@/api/ruleConfig/nomergeConfig'
export default {
  name: 'NoMergeDialog',
  data() {
    return {
      dialogVisible: false,
      title: '',
      infoForm: {},
      selectList: {},
      searchTags: Object.freeze([
        { label: 'LotId', prop: 'lotId', tag: 'DynInput' },
        { label: 'WaferId', prop: 'waferId', tag: 'DynTextarea', row: 2, placeholder: '多个以英文逗号隔开' },
        { label: this.$t('remark'), prop: 'remark', tag: 'DynTextarea', row: 2 }
      ]),
      rules: Object.freeze(
        {
          lotId: [{ required: true, message: `${this.$t('pleaseInput')}lotId`, trigger: 'blur' }],
          waferId: [{ required: true, message: `${this.$t('pleaseInput')}waferId`, trigger: 'blur' }]
        }
      )
    }
  },
  methods: {
    async handleOpen(row) {
      if (row && row.id) {
        this.title = this.$t('modify')
        const res = await getInfo({ id: row.id })
        const { data } = res
        this.infoForm = data
      } else {
        this.title = this.$t('add')
      }
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].$refs['dynForm'].clearValidate()
      })
    },
    doSave() {
      this.$refs['dataForm'].$refs['dynForm'].validate((valid) => {
        if (valid) {
          if (this.infoForm.id) {
            doUpdate(this.$clearNull(this.infoForm)).then(res => {
              this.$betterTips(res, this.handleClose, this.handleUpdate)
            })
          } else {
            doAdd(this.$clearNull(this.infoForm)).then(res => {
              this.$betterTips(res, this.handleClose, this.handleUpdate)
            })
          }
        }
      })
    },
    handleClose() {
      Object.assign(this.$data, this.$options.data.call(this))
    },
    handleUpdate() {
      this.$emit('initList')
    }
  }
}
</script>
